package servlet;

import entity.Booking;
import service.BookingService;
import service.impl.BookingServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/BookingServlet")
public class BookingServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();

        String opr = "list";
        int pageIndex = 1;
        int pageSize = 3;
        if (req.getParameter("opr")!=null){
            opr = req.getParameter("opr");
        }
        if (req.getParameter("pageIndex")!=null){
            pageIndex = Integer.parseInt( req.getParameter("pageIndex"));
        }
        List<Booking> list = null;

        BookingService service = new BookingServiceImpl();


        switch (opr){
            case "list":
                String userName = req.getParameter("userName");
                int status = -1;
                if (req.getParameter("status") != null && req.getParameter("status") != ""){
                    status = Integer.parseInt(req.getParameter("status"));
                }
                list = service.selectList(pageIndex,pageSize,userName,status);
                int pageCount = service.count(pageSize,userName,status);
//                System.out.println(pageCount);
                req.setAttribute("list",list);
                req.setAttribute("pageIndex",pageIndex);
                req.setAttribute("pageCount",pageCount);
                req.setAttribute("userName",userName);
                req.setAttribute("status",status);
                req.getRequestDispatcher("index.jsp").forward(req,resp);
                break;
            case "del":
                int r = service.delete(Integer.parseInt(req.getParameter("id")));
                if (r > 0)
                    out.print("删除成功");
                else
                    out.print("删除失败");
                break;
            case "che":
                String cardId = req.getParameter("cardId");
                r = service.selectBooking(cardId);
                if (r > 0)
                    out.print("该人员已接种或已预约疫苗");
                else
                    out.print("");
                break;
            case "add":
                userName = req.getParameter("userName");
                cardId = req.getParameter("cardId");
                String bookingDate = req.getParameter("bookingDate");
                Booking booking = new Booking(userName,cardId,bookingDate);
                r = service.insert(booking);
                out.print(r);
                break;
            case "update":
                int id = Integer.parseInt(req.getParameter("id"));
                r = service.update(id);
                if (r > 0){
                    out.print("接种成功");
                }else
                    out.print("接种失败");
                break;
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
